package com.fan.springboot1903fanjianghao.service.impl;

import com.fan.springboot1903fanjianghao.bean.User;
import com.fan.springboot1903fanjianghao.mapper.UserMapper;
import com.fan.springboot1903fanjianghao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * ZixiaFan
 * 2021/11/21 18:57
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    /**
     * 登录
     * @return 0:成功 1：今日错误次数超过五次，请明天再试" 2：密码错误 3：用户名不存在
     */
    public User login(User user){
        int res=-1;
        String s="";
        Date date=new Date();
        String t = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        //检查用户名是否存在
        User u = userMapper.queryUsernameExists(user.getUsername());
        if (u!=null){//存在 查看今日失败次数 >5 不允许登录
            if (u.getErrorcount()>=5){
                //检查次数有无过时
                User user1=u;
                u.setErrortime(t);
                Integer errorDay = userMapper.queryErrorDay(user1);
                if (errorDay>=1){
                    if (u.getPassword().equals(user.getPassword())){
                        user1.setErrorcount(0);
                        userMapper.updateErrorCount(user1);
                        s="登陆成功";
                        u.setErrorcount(0);
                        userMapper.updateErrorCount(u);
                        res=0;
                    }else {
                        user1.setErrortime(t);
                        user1.setErrorcount(1);
                        userMapper.updateCountTime(user1);
                    }
                }else {
                    s="今日错误次数超过五次，请明天再试";
                    res=1;
                }
            }else {//<5 验证用户名和密码是否对应
//                User user1 = userMapper.login(user);
                if (u.getPassword().equals(user.getPassword())){
                    s="登陆成功";
                    u.setErrorcount(0);
                    userMapper.updateErrorCount(u);
                    res=0;
                }else {
                    s="密码错误";
                    res=2;
                    u.setErrortime(t);
                    u.setErrorcount(1+u.getErrorcount());
                    userMapper.updateCountTime(u);
                }
            }
        }else {//不存在 提示 用户名是否存在
            s="用户名不存在";
            res=3;
        }
        user.setState(s);
        user.setStateCode(res);
        return user;
    }

    /**
     * 注册
     * @param user
     * @return
     */
    @Override
    public int register(User user) {
        Date date=new Date();
        String t = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        user.setCreatetime(t);
        return userMapper.register(user);
    }

    /**
     * 验证用户名是否唯一
     * @param username
     * @return
     */
    @Override
    public int UniqueUsername(String username) {
        User user = userMapper.queryUsernameExists(username);
        if (user!=null){
            return 0;
        }else {
            return 1;
        }
    }

    /**
     * 调用mapper层返回所有密保问题
     * @param flag
     * @return
     */
    @Override
    public List<String> queryQuestion(String flag) {
        return userMapper.queryQuestion(flag);
    }

    /**
     * 查询所有用户
     * @return List<user>
     */
    @Override
    public List<User> queryAllUser() {
        return userMapper.queryAllUser();
    }

    /**
     * 根据用户名模糊查询
     * @param QUsername
     * @return
     */
    @Override
    public List<User> queryUserByName(String QUsername) {
        return userMapper.queryUserByName(QUsername);
    }

    /**
     * 根据ID删除用户
     * @param id
     * @return
     */
    public int delUser(Integer id){
        return userMapper.delUser(id);
    }
}
